package com.xm.servlet.reception_desk.captcha;

import com.xm.util.SmsUtil;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Random;

/**
 * 发送手机验证码
 * @author 康文程
 * @date 2023-12-24 下午 01:22
 */
@WebServlet("/phone/captcha")
public class PhoneCaptcha extends HttpServlet {
    private final SmsUtil smsUtil = new SmsUtil();

    /**
     * 发送手机验证码
     * @param req   an {@link HttpServletRequest} object that
     *                  contains the request the client has made
     *                  of the servlet
     *
     * @param resp  an {@link HttpServletResponse} object that
     *                  contains the response the servlet sends
     *                  to the client
     *
     */
    @SneakyThrows(Exception.class)
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        String phone = req.getParameter("phone");
        Random random = new Random();
        int randomNumber = random.nextInt(9000) + 1000;
        Integer integer = SmsUtil.setSms(phone, random.nextInt(9000) + 1000);
        System.out.println(integer);
        resp.getWriter().println(integer);
    }
}
